module Main where

import LanguagePieces hiding(main)
-- import PPrintDataPieces
import ParsePieces( parseSTG )
import Utils
import DataPieces
import System( getArgs )

{- *** PROGRAMA *** -}
main :: IO ()
{- -}
main = do
         args <- getArgs
         (fI,fO) <- fileNames args
         progS <- readFile fI
         writeFile fO $ mainWorker progS   

-- Dado el texto a realizar el depiece devuelve el resultado del despiece.
mainWorker :: String -> String
mainWorker progS = toPieces.parseSTG $ progS
         
fileNames :: [String] -> IO (String, String)
fileNames args = case length args of
                       0 -> do
                              putStrLn "Nombre del fichero de entrada:"
                              fInputName <- getLine
                              putStrLn "Nombre del fichero de salida:"
                              fOutputName <- getLine
                              return (fInputName, fOutputName)
                       1 -> do
                              let fInputName = args !! 0
                              return (fInputName, (\(x,y) -> x++"Pieces"++y) $ span (/='.') fInputName)
                       _ -> return (args !! 0, args !! 1)


{- *** DEPURACION *** -}
-- main = testPieces tresNmasS
main0 = testParser test2S
-- main = testPPrint factS_
-- main2 = testPieces test2S


test0S = "fact n = case n of { 0 -> (3,4,5) ; otherwise -> let m = n-1 in n * fact m}"
test1S = "fact n = case n of { 0 -> 1 ; otherwise -> let m = n-1 in n * fact m}"
test2S = "fact n | n==0 = 1 | otherwise = n* fact (n-1)"

factS :: String
factS  = "fact n@0 = 1; fact n@n = n * fact (n-1)"
factS_  = "fact n@0 (x:xs) (x,y)= 1; fact n = n * fact (n-1)"
fact0S = "fact n = case n of { 0 -> 1 ; otherwise -> let m = n-1 in n * fact m}"
fact1S = "fact n = case n of {0 -> 1 ; otherwise -> n * fact (n-1)}"
fact2S = "fact n | n == 0 = 1 | otherwise  = n * fact (n-1) where {hola= adios}"


fibS :: String
fib  = "fib 0 = 1; fib 1 = 1; fib n = fib (n-1) + fib (n-2)"
fibS  = "fib n@0 = 1; fib n@1 = 1; fib n@n = fib (n-1) + fib (n-2)"
fib1S = "fib n = case n of {0 -> 1 ; 1 -> 1 ; otherwise -> fib (n-1) + fib (n-2)}"
fib1ES = "fib n = case y of {0 -> 1 ; 1 -> 1 ; otherwise -> fib (n-1) + fib (n-2)}"
fib2S = "fib n | n==0 || n==1 = 1 | otherwise = fib (n-1) + fib (n-2)"

{-
addNumbers :: [Int] -> Int
addNumbers [] = 0
addNumbers (x:xs) = x + addNumbers xs
-}

sum1S = "sum xs = case xs of {[] -> 0; otherwise -> head xs + sum (tail xs)}"
sum2S = "sum xs = case xs of {[] -> 0; (y:ys) -> y + sum ys}"
{-
tresNmas 1 = 0
tresNmas n 
   | even n = 1 + tresNmas (n `div` 2)
   | otherwise = 1 + tresNmas (3*n+1)
-}
tresNmasS = "tresNmas n@1 = 0; tresNmas n@n | even n = 1 + tresNmas (n `div` 2) | otherwise = 1 + tresNmas (3*n+1)"
tresNmas1S = "tresNmas n = case n of {1 -> 0; x -> if (even n) then (1 + tresNmas (n `div` 2)) else (1 + tresNmas (3*n+1))}"

isSortedTreeS = "isSortedTree t@(Empty) = True; isSortedTree t@(BinNode a t1 t2) = isSortedTree t1 && maxElem t1 < a && a < minElem t2 && isSortedTree t2"
isSortedTree1S = "isSortedTree t = case t of {Empty -> True; BinNode a t1 t2 -> isSortedTree t1 && maxElem t1 < a && a < minElem t2 && isSortedTree t2}"

quickS = 
  "quicksort ys@[] = []; \n"++
  "quicksort ys@(x:xs) = quicksort menores ++ [x] ++ quicksort mayores \n"++
  "  where { \n"++
  "     menores = filter (\\y-> y<x) xs; \n"++
  "     mayores = filter (\\y -> y>x) xs \n"++
  "     };\n"
quickS1 = 
  "quicksort ys@[] = []; \n"++
  "quicksort ys@(x:xs) = let \n"++
  "                        menores = filter (\\y-> y<x) xs; \n"++
  "                        mayores = filter (\\y-> y>x) xs; \n"++
  "                      in quicksort menores ++ [x] ++ quicksort mayores; \n"
quickS2 = 
  "quicksort ys@[] = []; \n"++
  "quicksort ys@(x:xs) =  ms ++ [x] ++ mys \n"++
  "  where { \n"++
  "     ms = quicksort menores; \n"++
  "     mys = quicksort mayores; \n"++
  "     menores = filter (\\y-> y<x) xs; \n"++
  "     mayores = filter (\\y -> y>x) xs \n"++
  "     };\n"
